#include "MoveGenerator.h"
#include "searchAlgo.h"
#include "step.h"
#include "record.h"
#include <cassert>

MoveGenerator::MoveGenerator(Go * pgo)
{
    assert(pgo!=0);
    goCore = pgo;
}

MoveGenerator::~MoveGenerator()
{
}

Go * MoveGenerator::createMirrorCore()
{
    //强制保证这个函数不会在复盘状态下被调用
    assert(goCore->game_status);

    Go * mirrorCore = new Go();

    if(goCore->tempLatestStep == 0) //表示goCore中的board为棋局初始状态尚未有着子
    {
        assert(goCore->block_index == 1); //Debugg use
        mirrorCore->changeHandicaps(goCore->handicapsValue); //包含了无让子的情况
    }
    else //在这种情况下在record中放入第一手棋的Step对象, 用于regret()时之用
    {
        if(goCore->block_index == 2)
        {
            mirrorCore->changeHandicaps(goCore->handicapsValue); //包含了无让子的情况

            //要考虑到为pass的情况
            int x = goCore->tempLatestStep->getCoordinateX();
            int y = goCore->tempLatestStep->getCoordinateY();
            if(x==0 && y==0) {mirrorCore->pass();}
            else
            {
                mirrorCore->doit(x,y);
            }
        }

        else
        {
            int i = goCore->block_index - 2;
            mirrorCore->recoverStep(goCore->record->getStep(i)); //将mirror还原到最后一步之前一步的状态

            int x = goCore->tempLatestStep->getCoordinateX();
            int y = goCore->tempLatestStep->getCoordinateY();
            if(x==0 && y==0) {mirrorCore->pass();}
            else
            {
                mirrorCore->doit(x,y);
            }
        }
    }

    //额外对_ban_moves进行拷贝
    mirrorCore->write_ban_moves(goCore->get_ban_moves());

    return mirrorCore;
}

coordinate MoveGenerator::runABMinMax()
{
    Go * mirrorCore = createMirrorCore();
    ABMinMax abMinMax = ABMinMax(mirrorCore);

    coordinate coo = abMinMax.alphaBetaMinMaxSearch();

    //at the end
    delete mirrorCore;

    return coo;
}
